/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.course.subscribe.model.ArrangedCourse;
import cn.com.chinasoa.course.subscribe.model.PrivilegeStrategy;
import cn.com.chinasoa.course.subscribe.model.TrainingAddress;
import cn.com.chinasoa.course.subscribe.model.TrainingSubject;
import cn.com.chinasoa.course.subscribe.util.DataTypeUtil;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-5下午03:01:26
 */
public class ArrangedCourseOperateActionFilter extends BaseRequestFilter
		implements ActionFilter {

	private static String SUCCESS = "success";
	private static String FAILED = "error";


	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {

		boolean success = false;

		long arrangedCourseId = DataTypeUtil.parseLong(actionRequest.getParameter("arrangedCourseId"));
		String cmd = actionRequest.getParameter("cmd");
		
		String courseTitle = actionRequest.getParameter("courseTitle");
		String courseDescription = actionRequest.getParameter("courseDescription");
		String trainingSubject = actionRequest.getParameter("trainingSubject");
		
		Calendar startDate = DataTypeUtil.parseCalender(actionRequest.getParameter("startDate"));
		Calendar endDate = DataTypeUtil.parseCalender(actionRequest.getParameter("endDate"));
		
		String trainingAddress = actionRequest.getParameter("trainingAddress");

		List<Long> privilegeStrategyIdList = DataTypeUtil.getParamLongList("privilegeStrategy_",actionRequest);
		
		if((cmd!=null)&&(arrangedCourseId>0)&&(courseTitle!=null)&&(courseDescription!=null)
				&&(trainingSubject!=null)&&(startDate!=null)&&(endDate!=null)&&(trainingAddress!=null)){
			if(cmd.equals("edit")){
				EntityManager em = EntityManagerHelper.getEntityManager();
				em.getTransaction().begin();
				
				ArrangedCourse vArrangedCourse = em.find(ArrangedCourse.class,arrangedCourseId);	
				if(vArrangedCourse!=null){
					vArrangedCourse.setCourseTitle(courseTitle);
					vArrangedCourse.setCourseDescription(courseDescription);
		
					TrainingSubject tTrainingSubject = em.find(TrainingSubject.class, DataTypeUtil.parseLong(trainingSubject));
					vArrangedCourse.setTrainingSubject(tTrainingSubject);
					
					TrainingAddress tTrainingAddress = em.find(TrainingAddress.class, DataTypeUtil.parseLong(trainingAddress));
					vArrangedCourse.setTrainingAddress(tTrainingAddress);
		
					vArrangedCourse.getPrivilegeStrategys().clear();
					for(Iterator<Long> psIt = privilegeStrategyIdList.iterator();psIt.hasNext();){
						PrivilegeStrategy tPrivilegeStrategy = em.find(PrivilegeStrategy.class,psIt.next());
						vArrangedCourse.getPrivilegeStrategys().add(tPrivilegeStrategy);
					}

					vArrangedCourse.setStartDate(startDate);
					vArrangedCourse.setEndDate(endDate);
				}
				em.getTransaction().commit();
				EntityManagerHelper.closeEntityManager();
				
				success = true;
			}
		}else{
			if((cmd!=null)&&(arrangedCourseId>0)&&(cmd.equals("delete"))){
				EntityManager em = EntityManagerHelper.getEntityManager();
				em.getTransaction().begin();
				ArrangedCourse vArrangedCourse = em.find(ArrangedCourse.class,arrangedCourseId);	
				if(vArrangedCourse!=null){
					em.remove(vArrangedCourse);
				}
				em.getTransaction().commit();
				EntityManagerHelper.closeEntityManager();	
				success = true;
			}else{
				success = false;
			}
		}

		if(success)return SUCCESS;
		else return FAILED;
	}

}
